<?php
class NewsC {
	function __construct(){
		FunctionsHelper::includeFiles(
			array(
				ADMINISTRATOR_MODEL_DIRECTORY . 'model.php',
				ADMINISTRATOR_MODEL_DIRECTORY . 'news.php'
			)
		);
	}

	public function getAll(){
		$arrNews = array();

		FunctionsHelper::includeFiles(
			array(
				ADMINISTRATOR_CONTROLLER_DIRECTORY . 'users.php'
			)
		);
		
		$arrResult = News::getAll();
		foreach($arrResult as $news){
			$users = new Users();
			$user = $users->getUser(array('user_id' => $news['user_id']));
			$news['user_id'] = $user;
			$arrNews[] = new News($news);
		}

		return $arrNews;
	}
	
	public function updateNews($arrInitialValues = null){
		$arrInitialValues['created'] = FunctionsHelper::adaptDBFormatDate($arrInitialValues['created']) . ' ' . date('H:i:s');;	

		if (empty($arrInitialValues))
			return 0;

		$news = new News($arrInitialValues);
		
		$news->update();
		return $news->getLastNewsId();
	}
	
	public function createNews($arrInitialValues = null){
		if (empty($arrInitialValues))
			return 0;

		$arrInitialValues['created'] = FunctionsHelper::adaptDBFormatDate($arrInitialValues['created']) . ' ' . date('H:i:s');	
		$news = new News($arrInitialValues);

		$news->create();
		return $news->getLastNewsId();
	}
	
	public function deleteNews($id){
		if (empty($id))
			return 0;
			
		$news = new News(
			array (
				'news_id' => $id
			)
		);
		
		return $news->delete();
	}
	
	public function activateNews($id){
		if (empty($id))
			return 0;
			
		$news = new News(
			array (
				'news_id' => $id
			)
		);
		
		return $news->activate();
	}
	
	public function getNews($arrInitialValues = null){
		if (empty($arrInitialValues))
			return new News();

		$news = new News($arrInitialValues);

		$news = $news->getNews();

		if (empty($news))
			return new News();

		$news = new News($news[0]);
		$news->setCreated(date('d/m/Y H:i:s', strtotime($news->getCreated())));
		$news->setImages($news->obtainImages());
		$news->setAudio($news->obtainAudio());
		$news->setVideo($news->obtainVideo());
		$news->setTag($news->obtainTag());
		return $news;
	}
	
	public function deleteAudio($id){
		$news = new News();
		$audioName = $news->getAudioWithId($id);
		$news->deleteAudio($audioName[0]['name_file']);
		return $news->clearAudio($id);
	}
	
	public function deleteVideo($id){
		$news = new News();
		$news->deleteVideo($id);
		return 1;
	}
	
	public function deleteTag($id){
		$news = new News();
		$news->deleteTag($id);
		return 1;
	}

	public function deleteImage($id){
		if (empty($id))
			return 1;
		
		$news = new News();
		$imageName = $news->getImageWithId($id);
		$news->deleteImage($imageName[0]['name_file']);
		return $news->clearImage($id);
	}

	public function deleteResource($id, $resource_type){
		if (empty($id) || empty($resource_type))
			return 1;
		
		$news = new News();
		
		switch($resource_type){
			case 'i':
				return $this->deleteImage($id);
			case 'a':
				return $this->deleteAudio($id);
			case 'v':
				return $this->deleteVideo($id);
			case 't':
				return $this->deleteTag($id);
		}
	}
	
	public function modifyResource($id, $resource_type, $data){
		if (empty($id) || empty($resource_type))
			return 1;
		
		$news = new News();
		
		switch($resource_type){
			case 'i': 
				return $news->updateResourceData('images', $id, $data['text'], $data['tag']);
			case 'a':
				return $news->updateResourceData('audio', $id, $data['text'], $data['tag']);
			case 'v':
				return $news->updateResourceData('video', $id, $data['text'], $data['tag']);
		}
		
		$this->updateResource($which);
	}

	public function modifyText($id, $text, $tag){
		if (empty($id))
			return 1;

		$news = new News();
		$arrImage = array ($id, $text, $tag);
		return $news->updateTextImage($arrImage);
	}
	
	public function InsertResource($NewsId, $ArrResources = null){
		if (empty($ArrResources) || empty($NewsId) || !array_key_exists('texts', $ArrResources))
			return;
		$news = new News();

		$resource_type = $ArrResources['type'];
		for($i = 1; $i <= count($ArrResources['texts']); $i++){
			if ($resource_type == 'videos'){
				if (!empty($ArrResources['locations']['video_link_' . $i]))
					$news->InsertVideo($NewsId, FunctionsHelper::stripYouTubeVideoPrefix($ArrResources['locations']['video_link_' . $i]), $ArrResources['texts']['video_text_' . $i], $ArrResources['tags']['video_tag_' . $i]);
			}elseif ($resource_type == 'audios'){
				if ($ArrResources['locations']['audio_filename_' . $i]['error'] == 0 && copy($ArrResources['locations']['audio_filename_' . $i]['tmp_name'], MAIN_FILES_DIRECTORY . FunctionsHelper::filesFormatName($ArrResources['locations']['audio_filename_' . $i]['name'])))
					$news->InsertAudio($NewsId, stripslashes($ArrResources['locations']['audio_filename_' . $i]), $ArrResources['texts']['audio_text_' . $i], $ArrResources['tags']['audio_tag_' . $i]);
			}elseif ($resource_type == 'images'){
				if ($ArrResources['locations']['image_filename_' . $i]['error'] == 0 && copy($ArrResources['locations']['image_filename_' . $i]['tmp_name'], MAIN_IMAGES_NEWS_DIRECTORY . FunctionsHelper::filesFormatName($ArrResources['locations']['image_filename_' . $i]['name'])))
					$news->InsertImage($NewsId, $ArrResources['locations']['image_filename_' . $i]['name'], $ArrResources['texts']['image_text_' . $i], $ArrResources['tags']['image_tag_' . $i]);
			}elseif ($resource_type == 'tags'){
				if (!empty($ArrResources['texts']['tag_text_' . $i]))
					$news->InsertTag($NewsId, $ArrResources['texts']['tag_text_' . $i]);
			}
		}
	}
		
	public function InsertImage($arrInitialValues = null){
		$news = new News($arrInitialValues);
		if (!empty($arrInitialValues['image'])){
			if (copy($arrInitialValues['image'][0]['tmp_name'], MAIN_IMAGES_NEWS_DIRECTORY . FunctionsHelper::filesFormatName($arrInitialValues['image'][0]['name']))){
				$news->InsertImage(array(FunctionsHelper::filesFormatName($arrInitialValues['image'][0]['name']), $arrInitialValues['image'][1], $arrInitialValues['image'][2]));
			}
		}
	}
	
	public function InsertAudio($arrInitialValues = null){
		$news = new News($arrInitialValues);
		if (!empty($arrInitialValues['audio'])){
			if (copy($arrInitialValues['audio'][0]['tmp_name'], MAIN_FILES_DIRECTORY . FunctionsHelper::filesFormatName($arrInitialValues['audio'][0]['name']))){
				$news->InsertAudio($arrInitialValues['news_id'], FunctionsHelper::filesFormatName($arrInitialValues['audio'][0]['name']));
			}
		}
	}
	
}

?>